package handlers

import (
	"car-order/cache"
	"car-order/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func CarAdd(c *gin.Context) {
	carName := c.PostForm("carName")
	carType := c.PostForm("carType")
	carPrice := c.PostForm("carPrice")
	if carName == "" || carType == "" || carPrice == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	car := models.Car{
		CarName:  carName,
		CarType:  carType,
		CarPrice: carPrice,
	}
	err := models.DB.Create(&car).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "车辆信息添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "车辆信息添加成功",
	})
	return
}

func CarOrderAdd(c *gin.Context) {
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	orderCode := c.PostForm("orderCode")
	orderPrice := c.PostForm("orderPrice")
	carOrder := models.CarOrder{
		Cid:        cid,
		OrderCode:  orderCode,
		OrderPrice: orderPrice,
	}
	err := models.DB.Create(&carOrder).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "车辆订单添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "车辆订单添加成功",
	})
	return
}

func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var carOrderList []models.CarOrderList
	redis_key := "carOrderList" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cars").
			Select("car_orders.id AS OrderId",
				"cars.car_name AS CarName",
				"cars.car_type AS CarType",
				"cars.car_price AS CarPrice",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_price AS OrderPrice",
			).
			Joins("LEFT JOIN car_orders ON car_orders.cid = cars.id").
			Offset(offset).
			Limit(size).
			Find(&carOrderList)
		jsonData, _ := json.Marshal(carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderList)
	}

	var count int64
	models.DB.Model(&models.CarOrder{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": gin.H{
			"count": count,
			"data":  carOrderList,
		},
	})
}

func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")

	var carOrderList models.CarOrderList
	redis_key := "carOrderDetail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("cars").
			Select("car_orders.id AS OrderId",
				"cars.car_name AS CarName",
				"cars.car_type AS CarType",
				"cars.car_price AS CarPrice",
				"car_orders.order_code AS OrderCode",
				"car_orders.order_price AS OrderPrice",
			).
			Where("car_orders.cid = ?", id).
			Joins("LEFT JOIN car_orders ON car_orders.cid = cars.id").
			Find(&carOrderList)
		jsonData, _ := json.Marshal(carOrderList)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &carOrderList)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
		"data": carOrderList,
	})
}
